#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char* argv[]){
  if(argc == 1){
    printf("command wrong!\n");
  }
  else{
    int fd;
    int std_out;
    if(argv[argc-1][0] == '>'){
      char *output_file;
      output_file = strtok(argv[argc-1],">");
      fd = open(output_file, O_CREAT | O_RDWR | O_TRUNC, 0666);
      std_out = dup(1);
      dup2(fd,1);
      close(fd);
      argv[argc-1] = NULL;
      argc -= 1;
    }
    int len = 0;
    for(int i=1;i<argc;i++){
      len += strlen(argv[i]);
    }
    len += argc-2;
    char *str = (char*)malloc(len);
    for(int i=1;i<argc;i++){
      strcat(str,argv[i]);
      if(i != argc-1) strcat(str," ");
    }
    write(1,str,len);
    printf("\n");
  }
  return 0;
}